﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using XinYue.Permission.Mode;

namespace XinYue.Permission.DAL
{
  public  class BaseRepository<T,BS> where T:class where BS:DbContext,new()
    {
        //当前操作数据库实例
        private DbContext db = DbContextFactory<BS>.GetGurrentDbContext();

        #region 添加数据
        /// <summary>
        /// 添加单条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {
            db.Set<T>().Add(entity);
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 添加多条记录
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        public bool AddRange(List<T> entitys)
        {
            db.Set<T>().AddRange(entitys);
            return db.SaveChanges() > 0;
        }
        #endregion

        #region 修改数据
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            db.Entry(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        } 
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除单条数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            db.Entry(entity).State = EntityState.Deleted;
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool BatchDelete(List<T> entitys)
        {
            db.Set<T>().RemoveRange(entitys);
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 根据ID批量删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool BatchDeleteByID(params int[] IDs)
        {
            foreach (var id in IDs)
            {
                var entity = db.Set<T>().Find(id);
                if (entity != null) db.Set<T>().Remove(entity);
            }
            return db.SaveChanges() > 0;
        }
        #endregion

        #region 查询数据
        /// <summary>
        /// 查询返回一个集合
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public List<T> QueryList(Expression<Func<T, bool>> lambdaExpression)
        {
            return db.Set<T>().Where(lambdaExpression).ToList();
        }
        /// <summary>
        /// 查询返回一个对象
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public T Query(Expression<Func<T, bool>> lambdaExpression)
        {
            return db.Set<T>().SingleOrDefault(lambdaExpression);
        }
        /// <summary>
        /// 查询是否存在
        /// </summary>
        /// <param name="lambdaExpression"></param>
        /// <returns></returns>
        public bool Exists(Expression<Func<T, bool>> lambdaExpression)
        {
            return db.Set<T>().Any(lambdaExpression);
        } ///
        #endregion

        #region 查询分页
        public List<T> QueryPageList<S>(int PageIndex, int PageSize, Expression<Func<T, bool>> Wherelambda, Expression<Func<T, S>> OrderBylambda, out int count, bool isAsc = true)
        {
            //获取总数
            count = db.Set<T>().Where(Wherelambda).Count();

            if (!isAsc)
                return db.Set<T>().Where(Wherelambda)
                    .OrderByDescending(OrderBylambda)
                    .Skip((PageIndex - 1) * PageSize)
                    .Take(PageSize).ToList();

            return db.Set<T>().Where(Wherelambda)
                   .OrderBy(OrderBylambda)
                   .Skip((PageIndex - 1) * PageSize)
                   .Take(PageSize).ToList();
        } 
        #endregion
    }
}
